package cn.hrc.controller;

import cn.hrc.dao.MarkDao;
import cn.hrc.dao.TaskDao;
import cn.hrc.dao.UserDao;
import cn.hrc.entity.MarkInfo;
import cn.hrc.entity.TaskInfo;
import cn.hrc.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/stu")
public class StuController {
    @Autowired
    TaskDao taskDao;
    @Autowired
    MarkDao markDao;
    @Autowired
    UserDao userDao;
    /** 去更改通过保存的session.uid进行查询
     * 【不在页面直接使用session.who.*】 */
    @RequestMapping("/go2updateMy")
    public String go2updateMe(HttpSession session, Model model){
        User who = (User) session.getAttribute("who");
        if(who == null){
            model.addAttribute("loginmsg","请先登录在调用该功能");
            return "login";
        }
        User user  = userDao.findByUid(who.getUid());
        model.addAttribute("user",user);
        return "stu/myUpdate";
    }
    /** 学生更改密码【加了姓名】 */
    @PostMapping("/updateMy")
    public String updateMe(User user,HttpSession session, Model model){
        User who = (User) session.getAttribute("who");
        if(who == null){
            model.addAttribute("loginmsg","请先登录在调用该功能");
            return "login";
        }
        user.setUid(who.getUid());
        int num = userDao.updateUser(user);
        System.out.println("更改成功");
        return "forward:/home/logout";
    }
    /** 学生所在班级的课程 */
    @RequestMapping("/findMyCourse")
    public String findMyCourse(HttpSession session, Model model){
        User user = (User) session.getAttribute("who");
        if (user == null){
            model.addAttribute("loginmsg","请先登录在调用该功能");
            return "login";
        }
        /** 获取学生课程 */
        List<TaskInfo> tasks = taskDao.findByClzno(user.getClzno());
        model.addAttribute("tasks",tasks);
        return "stu/myTask";
    }
    /** 学生在班级所有课程的成绩 */
    @RequestMapping("findMyMark")
    public String findMyMark(HttpSession session, Model model){
        User user = (User) session.getAttribute("who");
        if (user == null){
            model.addAttribute("loginmsg","请先登录在调用该功能");
            return "login";
        }
        /** 获取学生的成绩 */
        List<MarkInfo> marks = markDao.findMyMark(user.getPhone());
        System.out.println("学生成绩\n"+marks);
        model.addAttribute("marks",marks);
        return "stu/myMark";
    }

}
